package com.evaluation.kexingcp.psycho.evaluation.sys.evaluation

import com.alibaba.fastjson.JSON
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.deepoove.poi.XWPFTemplate
import com.deepoove.poi.config.Configure
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy
import com.evaluation.kexingcp.common.security.annotation.Inner
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationQuestionsEntity
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationQuestionsService
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationScaleService
import jakarta.servlet.http.HttpServletResponse
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URLEncoder


class Question {
    var subject: String? = null
    var ismultiple: Boolean = false
    var id: Long = 0
    var option: List<Option> = mutableListOf()
}

class Option {
    var id: Long = 0
    var selected: String? = null
    var checked: Boolean = false
    var score: String? = null
}

@RestController
@RequestMapping("/scale")
open class ScaleController(
    private val evaluationScaleService: EvaluationScaleService,
    private val evaluationQuestionsService: EvaluationQuestionsService,
) {

    @Value("classpath:scale_template.docx") // resources 目录下的文件
    lateinit var templateResource: Resource

    @GetMapping("/export/{scaleId}")
    @Inner(value = false)
    open fun evaluationScale(@PathVariable("scaleId") scaleId: Long, response: HttpServletResponse) {
        val policy = LoopRowTableRenderPolicy()

        val config = Configure.builder()
            .bind("factorResults", policy)
            .useSpringEL()
            .build()

        val scale = evaluationScaleService.getById(scaleId)
        val questionEntities = evaluationQuestionsService.list(QueryWrapper<EvaluationQuestionsEntity>().eq("scale_id", scaleId))

        val questions = questionEntities.map { entity ->
            JSON.parseObject(entity.scaleQuestions, Question::class.java)
        }.toCollection(mutableListOf())

        val template: XWPFTemplate = XWPFTemplate.compile(templateResource.inputStream, config).render(
            object : HashMap<String?, Any?>() {
                init {
                    put("questions", questions)
                    put("scale", scale)
                }
            }
        )

        response.characterEncoding = "utf-8";
        response.contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8";
        response.setHeader("Access-Control-Expose-Headers","Content-disposition");
        response.setHeader("Content-disposition","attachment; filename=" +
                URLEncoder.encode(scale.scaleName.plus(".docx"), "UTF-8"));
        template.writeAndClose(response.outputStream)
    }
}